<?php
class ConnectersController extends Controller {
	public function index() {
		$this->getSmarty ();
		$this->smarty->display ( "admin/connecters.tpl" );
	}
// 	public function getpage() {
// 		$this->getSmarty ();
// 		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
// 		// START 数据库查询及分页数据
// 		$userService = new UsersService( $this->getDB () );
// 		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
// 		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
// 		$offset = ($page - 1) * $rows;
// 		$result = array ();
// 		$row_count = $userService->countNum ();
// 		$result ["total"] = $row_count;
// 		$user_infos = $userService->userPage ( $offset, $rows );
// 		$result ["rows"] = $user_infos;
// 		echo json_encode ( $result );
// 	}
	
	public function saveConnects(){
		require_once SERVICE . DS . 'admin' . DS . 'ConnectersService.class.php';
		// START 数据库查询及分页数据
		$service = new ConnectersService( $this->getDB () );
		$id = uniqid();
		$name = $_POST['name'];
		$email = $_POST['email'];
		$phone = $_POST['phone'];
		$website = $_POST['website'];
		$message = $_POST['message'];
	
		if(empty($email)){
			echo 0;
		}
		$sql = "INSERT INTO connects(id,name,email,phone,website,message,date) VALUES('$id','$name','$email','$phone','$website','$message',SYSDATE())";
		$result = $service->saveConnects($sql);
	
		require_once COMMON . DS . 'SendMailUtil.class.php';
		$adress = "feiya.gao@yivadigital.com";
		$title = "Yiva has a new contact !!";
		$content = "Name:".$name."<br>Email:".$email."<br>Phone:".$phone."<br>WebSite:".$website."<br>Message:".$message;
		$sendstatus = SendMailUtil::sendmail ( $title, $content, $adress );
		echo json_encode ( $result );
	
	}
	
	
	function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ConnectersService.class.php';
		// START 数据库查询及分页数据
		$userService = new ConnectersService( $this->getDB () );
		$id =trim ($_POST ['id']);
		 $userService->delete($id);
		 echo -1;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'ConnectersService.class.php';
		// START 数据库查询及分页数据
		$userService = new ConnectersService( $this->getDB () );
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$website =trim ($_POST ['website']);
		$name = trim($_POST ['name']);
		$email = trim($_POST ['email']);
		$condition = $this->getCondition ( $website, $name, $email );
		$offset = ($page - 1) * $rows;
		$result = array ();
	
		$row_count = $userService->countNum ();
		$result ["total"] = $row_count;
		$resource_infos = $userService->resourcePage ( $offset, $rows, $condition );
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function getCondition($website, $name, $email) {
		$condition = array ();
		if (! empty ( $website )) {
			$condition ['website'] = "website like '%" . $website . "%'";
		}
		if (! empty ( $name )) {
			$condition ['name'] = "name like '%" . $name . "%'";
		}
		if (! empty ( $email )) {
			$condition ['email'] = "email like '%" . $email . "%'";
		}
		 
		$i = 0;
		foreach ( $condition as $key => $value ) {
				
			if ($i == count ( $condition ) - 1) {
				$conditions .= $value;
			} else {
				$conditions .= $value . " and ";
			}
			$i ++;
		}
		return $conditions;
	}
}